var login_form = $("#login-form");

const fields = {
  //校验用户名，对应name表单的name属性
  username: {
    validators: {
      //不能为空
      notEmpty: {
        message: "用户名不能为空"
      },
      //长度校验
      stringLength: {
        min: 4,
        max: 30,
        message: "用户名长度必须在4到30之间"
      },
      //正则校验
      regexp: {
        regexp: /^[a-zA-Z0-9_\.]+$/,
        message: "用户名由数字字母下划线和.组成"
      },
      callback: {
        message: "用户名错误"
      }
    }
  },
  // 校验密码
  password: {
    validators: {
      notEmpty: {
        message: "密码不能为空"
      },
      //长度校验
      stringLength: {
        min: 4,
        max: 30,
        message: "密码长度必须在4到30之间"
      },
      //正则校验
      regexp: {
        regexp: /^[a-zA-Z0-9]+$/,
        message: "用户名由数字字母组成"
      },
      callback: {
        message: "密码错误"
      }
    }
  }
};
//表单验证
const valid = new Vaild(login_form, fields, login);

var validator = login_form.data("bootstrapValidator"); //获取表单校验实例

//登录
function login() {
  $.ajax({
    url: "/employee/employeeLogin",
    type: "POST",
    data: login_form.serialize(),
    dateType: "json",
    success: function (res) {
      if (res.success) {
        // 跳转到首页
        location.href = "./index.html";
      } else if (res.error == 1000) {
        login_form
          .data("bootstrapValidator")
          .updateStatus("username", "INVALID", "callback");
      } else {
        login_form
          .data("bootstrapValidator")
          .updateStatus("password", "INVALID", "callback");
      }
    },
    fail: function (err) {
      console.log(err);
    }
  });
}
